package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Comment;
import com.ruoyi.system.mapper.ArticleMapper;
import com.ruoyi.system.mapper.CommentMapper;
import com.ruoyi.system.service.CommentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentDao;

    @Autowired
    private ArticleMapper articleDao;

    private List<Comment> tempReplyPlus = new ArrayList<>();

    private List<Comment> tempReplyPlusS = new ArrayList<>();

    @Override
    public int saveComment(Comment comment) {
        int comments = commentDao.saveComment(comment);
        articleDao.getCommentCountById(comment.getBlogId());
        return comments;
    }

    @Override
    public void deleteComment(Comment comment, Long id) {
        List<Comment> byparentCommentId = commentDao.findByparentCommentId(id);
        commentDao.deleteComment(id);
        if(!byparentCommentId.isEmpty()){
            for(Comment c : byparentCommentId){
                commentDao.deleteComment(c.getId());
                if(c.getParentCommentId()!=null){
                    List<Comment> byparentCommentId1 = commentDao.findByparentCommentId(c.getParentCommentId());
                    for(Comment cc : byparentCommentId1){
                        commentDao.deleteComment(cc.getId());
                    }

                }
            }
        }
        articleDao.getCommentCountById(comment.getBlogId());
    }

    @Override
    public List<Comment> findByparentCommentId( Long parentCommentId){
        return commentDao.findByparentCommentId(parentCommentId);
    }

    @Override
    public List<Comment> listCommentByBlogId(Long blogId) {
        //查询出父节点
        List<Comment> comments = commentDao.findByBlogIdParentIdNull(blogId, Long.parseLong("-1"));
        for(Comment comment : comments){
            Long id = comment.getId();
            String parentNickname1 = comment.getNickname();
            List<Comment> childComments = commentDao.findByBlogIdParentIdNotNull(blogId,id);
            //查询出子评论
            combineChildren(blogId, childComments, parentNickname1);
            comment.setReplyComments(tempReplyPlus);
            tempReplyPlus = new ArrayList<>();
        }
        return comments;
    }

    private void combineChildren(Long blogId, List<Comment> childComments, String parentNickname1) {
        //判断是否有一级子评论
        if(childComments.size() > 0){
            //循环找出子评论的id
            for(Comment childComment : childComments){
                String parentNickname = childComment.getNickname();
                childComment.setParentNickname(parentNickname1);
                tempReplyPlus.add(childComment);
                Long childId = childComment.getId();
                //查询出子二级评论
                recursively(blogId, childId, parentNickname);
            }
        }
    }

    private void recursively(Long blogId, Long childId, String parentNickname1) {
        //根据子一级评论的id找到子二级评论
        List<Comment> replayComments = commentDao.findByBlogIdAndReplayId(blogId,childId);

        if(replayComments.size() > 0){
            for(Comment replayComment : replayComments){
                String parentNickname = replayComment.getNickname();
                replayComment.setParentNickname(parentNickname1);
                Long replayId = replayComment.getId();
                tempReplyPlus.add(replayComment);
                recursively(blogId,replayId,parentNickname);
            }
        }
    }

    /*==============================================================================*/

    @Override
    public List<Comment> getCommentByUserId(Long userId) {
        List<Comment> comments = commentDao.findByUserIdParentIdNull(userId, Long.parseLong("-1"));
        for(Comment comment : comments){
            Long id = comment.getId();
            String parentNickname1 = comment.getNickname();
            List<Comment> childComments = commentDao.findByUserIdParentIdNotNull(userId,id);
            //查询出子评论
            combineChildrenS(userId, childComments, parentNickname1);
            comment.setReplyComments(tempReplyPlusS);
            tempReplyPlusS = new ArrayList<>();
        }
        return comments;
    }



    private void combineChildrenS(Long userId, List<Comment> childComments, String parentNickname1) {
        //判断是否有一级子评论
        if(childComments.size() > 0){
            //循环找出子评论的id
            for(Comment childComment : childComments){
                String parentNickname = childComment.getNickname();
                childComment.setParentNickname(parentNickname1);
                tempReplyPlusS.add(childComment);
                Long childId = childComment.getId();
                //查询出子二级评论
                recursivelyS(userId, childId, parentNickname);
            }
        }
    }

    private void recursivelyS(Long userId, Long childId, String parentNickname1) {
        //根据子一级评论的id找到子二级评论
        List<Comment> replayComments = commentDao.findByUserIdAndReplayId(userId,childId);

        if(replayComments.size() > 0){
            for(Comment replayComment : replayComments){
                String parentNickname = replayComment.getNickname();
                replayComment.setParentNickname(parentNickname1);
                Long replayId = replayComment.getId();
                tempReplyPlusS.add(replayComment);
                recursively(userId,replayId,parentNickname);
            }
        }
    }



}
